import sys
import paramiko
import requests
from framework.Function.ModbusBasicFunction.function import *
"""
米家设备控制开关 (如有)
"""

# # call_service 使用方法 在本地电脑运行local_service.py并开启CCProxy 最后在此处调用call_service()
# def call_service():
#     # ip为本地电脑ip 查询方式 cmd ipconfig 复制ipv4地址 端口为本地电脑local_service.py中定义的服务端口 app.run(host="0.0.0.0", port=5000) 则端口为5000
#     url = "http://192.168.32.86:5000/reboot-mijia"
#     try:
#         response = requests.post(url)
#         if response.status_code == 200:
#             print("调用成功：", response.json())
#         else:
#             print("调用失败：", response.json())
#     except Exception as e:
#         print("请求出错：", e)


# 在192.168.32.158控制器中执行python3.11 reboot_mijia.py
hostname = '192.168.32.158'
username = 'root'
password = 'passwd'
script_path = '/home/MIJIA/mijia-api-main_c/reboot_mijia.py'


# 程序包替换
# 配置 SSH 隧道和连接信息
ssh_host = '192.168.32.156'  # SSH服务器IP
ssh_port = 22  # SSH端口
ssh_user = 'root'  # SSH用户名
ssh_password = 'passwd'  # SSH密码
remote_package_path = '/home/LCS/'


def execute_reboot_mijia(hostname, username, password, script_path):
    """
    :param hostname: 远程控制器的 IP 地址或主机名
    :param username: SSH 用户名
    :param password: SSH 密码
    :param script_path: 远程 Python 脚本路径
    :return: 执行结果的输出和错误信息
    """
    try:
        # 创建 SSH 客户端
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # 连接到远程控制器
        ssh.connect(hostname=hostname, username=username, password=password)

        # 构建执行命令
        command = f"python3.11 {script_path}"

        # 执行命令
        stdin, stdout, stderr = ssh.exec_command(command)

        # 获取输出和错误信息
        output = stdout.read().decode()
        error = stderr.read().decode()

        # 打印结果
        if error:
            print(f"远程{script_path}执行失败：", error)
        else:
            print(f"远程{script_path}执行成功：", output)

        # 关闭 SSH 连接
        ssh.close()

    except Exception as e:
        return None, f"SSH 连接或命令执行失败: {str(e)}"


# 配方切换
# 0:默认配方
# 1-7:其它配方
def Formula_switch(formula_id):
    formula_switch(formula_id)


# 程序包写入
def package_write(host, user, passwd, package_location, remote_package_location):
    """
        :param host: 远程控制器的 IP 地址或主机名
        :param user: SSH 用户名
        :param passwd: SSH 密码
        :param package_location: 本地程序包位置
        :param remote_package_location: 程序包本地地址
        :return: 执行结果的输出和错误信息
    """
    try:
        # 创建 SSH 客户端
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # 连接到远程控制器
        ssh.connect(hostname=host, username=user, password=passwd)

        # 构建执行命令
        command1 = "cd /home/LCS"
        command0 = "killall s-Tunning LcsServer1_1"
        command2 = "cp scomm.so /usr/lib"

        # 执行命令1,0
        stdin1, stdout1, stderr1 = ssh.exec_command(command1)
        stdin0, stdout0, stderr0 = ssh.exec_command(command0)

        # 获取输出和错误信息
        output1 = stdout1.read().decode()
        error1 = stderr1.read().decode()
        output0 = stdout0.read().decode()
        error0 = stderr0.read().decode()

        # 程序包替换
        FileUtils.replace_file(package_location, remote_package_location, host, user, passwd)

        # 执行命令2
        stdin2, stdout2, stderr2 = ssh.exec_command(command2)

        # 获取输出和错误信息
        output2 = stdout2.read().decode()
        error2 = stderr2.read().decode()

        # 打印结果1
        if error:
            print(f"cd /home/LCS执行失败：", error1)
        else:
            print(f"cd /home/LCS执行成功：", output1)

        # 打印结果0
        if error:
            print(f"killall s-Tunning LcsServer1_1执行失败：", error0)
        else:
            print(f"killall s-Tunning LcsServer1_1执行成功：", output0)

        # 打印结果2
        if error:
            print(f"cp scomm.so /usr/lib执行失败：", error2)
        else:
            print(f"cp scomm.so /usr/lib执行成功：", output2)

        # 关闭 SSH 连接
        ssh.close()

    except Exception as e:
        return None, f"SSH 连接或命令执行失败: {str(e)}"


if __name__ == "__main__":
    package_location = r"C:\Users\neiwangji32\Desktop\my\岔道程序包\LCS"
    package_write(ssh_host, ssh_user, ssh_password, package_location, remote_package_path)
    # execute_reboot_mijia(hostname, username, password, script_path)

